fn main() {
    println!("Hello, world!");
}


// Vector
#[test]
fn test_vec() {
    //
    let vec_str: Vec<i32> = Vec::new();

    // 使用vec 宏
    let vec2 = vec![11, 22, 333];


    let mut vec3 = vec![11];

    //添加元素
    vec3.push(333);

    println!("vec is {:?}", vec3);


    // 获取vec 的元素
    /*
    按照索引  越界会报错
    get  越界不会报错，因为返回的Option
    */

    // let third = &vec3[2];   // 会报错


    let aa = vec3.get(2);

    let third = match vec3.get(2) {
        Some(x) => x,
        None => &0
    };


    println!("third is {}", third);


    // vec 借用   可变借用和不可变借用不能作用于一个变量， 在vec 同样适用，


    let mut v = vec![11, 22, 33, 44];

    let first = &v[0];

    // v.push(44);  // 这里报错 （mutable borrow occurs here）

    println!("first is {}", first);


    // 遍历vec     vec3 前面要加上&
    for i in &vec3 {
        println!("ele is {}", i);
    }


    println!(" vec3 is {:?}", vec3);
}


// String

#[test]
fn test_string() {


    //  String 的创建
    let str = String::new();

    let str = "aaaa".to_string();

    let str = String::from("hello ");


    // 更新String

    let mut string_builder = String::new();

    string_builder.push_str("aaaa");

    let bbb = String::from("_bbb");
    string_builder.push_str(&bbb);

    string_builder.push('A');


    println!("string_builder is {} , bbb is {}", string_builder, bbb);


    // 字符串拼接

    // 1. 加号后面的字符串要加 &  ,加号前面的变量失去了所有权

    let s1 = String::from("hello ");


    let s2 = String::from("world");


    let s3 = s1 + &s2;
    // println!(" s1 is {}", s1);  报错 value borrowed here after move

    println!("s2 is {} ,  s3 is {}", s2, s3);

    // 2. 使用 format!  , 字符串的所有权还在

    let f1 = String::from("f1");
    let f2 = String::from("f2");
    let f3 = String::from("f3");

    let f4 = format!("{}-{}-{}", f1, f2, f3);

    println!(" f1 is {} , f2 is {} , f3 is {} , f4 is {} ", f1, f2, f3, f4);


    let s1 = String::from("aaa");


    let s2 = String::from("你好");

    let len1 = s1.len();
    let len2 = s2.len();

    println!(" len1 is {} , len2 is {}", len1, len2);


    //String 中的字节(Bytes) 标量值(Scalar Values)

    println!("---------------------");

    let str = String::from("مرحبا");

    //  获取字符串的字节
    for b in str.bytes() {
        println!("b is {}", b);
    }


    // 获取字符串的unicode
    for c in str.chars() {
        println!("c is {}", c);
    }


    // String 的切割  谨慎使用， 要按照字符的边界切割， 否则会报错
    let substr = &str[0..2];
    println!("substr is {}", substr);


    // 遍历String
    //1. 获取字节 bytes()方法
    //2. 获取标量值  chars()方法
}


use std::collections::HashMap;

#[test]
fn test_hash_map() {
    let mut hash_map: HashMap<String, String> = HashMap::new();


    println!("hash_map is {:?}", hash_map);

    // 插入KV
    hash_map.insert("aaa".to_string(), "bbb".to_string());
    println!("hash_map is {:?}", hash_map);


    //  get 方法   返回Option
    let val = hash_map.get(&"aaa".to_string());
    println!("val is {}", val.unwrap());


    //  hashmap 和所有权
    /*
    1. 实现了Copy Trait 的类型， 值会复制到hashmap上
    2. 拥有所有权的值， 值会移动 ， 所有权也会转移给hashmap
    3. 将引用插入hash_map  ,所有权还在
    */


    let k1 = String::from("k1");
    let v1 = String::from("v1");
    let mut hash_map2: HashMap<String, String> = HashMap::new();
    hash_map2.insert(k1, v1);
    println!("hash_map2 is {:?}", hash_map2);

    // println!(" k1 is {} , v1 is {}", k1, v1);//  报错  value borrowed here after move


    let k1 = String::from("k1");
    let v1 = String::from("v1");
    let mut hash_map2: HashMap<&String, &String> = HashMap::new();
    hash_map2.insert(&k1, &v1);
    println!("--------------------hash_map2 is {:?}", hash_map2);
    println!("----------------- k1 is {} , v1 is {}", k1, v1);


    // hash_map 的遍历

    let mut map3 = HashMap::new();

    map3.insert(String::from("aaa"), 11);
    map3.insert(String::from("bbb"), 22);
    map3.insert(String::from("ccc"), 33);

    for (k, v) in &map3 {
        println!(" k is {} , v is {}", k, v);
    }


    // 更新hash_map
    // 1. 覆盖
    let mut map4 = HashMap::new();
    map4.insert(String::from("aaa"), 11);
    map4.insert(String::from("aaa"), 33);


    println!("map4 is {:?} ", map4);

    //2.  只有k 不存在的情况子下才插入  ，存在就不插入


    let mut map5 = HashMap::new();
    map5.insert(String::from("Blue"), 11);
    map5.entry(String::from("Yellow")).or_insert(444);//   如果部存在Yellow ， 就插入 ， key 是Yellow ， value 是444


    //3.
    
    
    
    
    
}


